package utils {
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import manager.LoggerManager;
	
	import sunix.animationPlayer.data.FrameData;

	public class BitmapUtils{	
		public static function cropBitmaps(bitmaps:Vector.<BitmapData>):Vector.<FrameData>{
			var frames:Vector.<FrameData> = new Vector.<FrameData>();
			for each(var bitmap:BitmapData in bitmaps){
				var frameData:FrameData = BitmapUtils.cropBitMap(bitmap);
				frames.push(frameData);
			}
			LoggerManager.log("裁减图片资源完成");
			return frames;
		}
			
		public static function cropBitMap(bitmap:BitmapData):FrameData{
			var center:Point=new Point(bitmap.width>>1, bitmap.height>>1);
			var tempBmd:BitmapData=new BitmapData(bitmap.width,bitmap.height,true,0x00000000);
			tempBmd.draw(bitmap);
			var rect:Rectangle = tempBmd.getColorBoundsRect(0xff000000,0x00000000,false);
			//空白图占位
			if(rect.width == 0 || rect.height == 0){
				rect.width = rect.height = 10;
			}	
			var bmd:BitmapData=new BitmapData(rect.width, rect.height, true, 0x00000000);
			bmd.draw(bitmap, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, null, true);
			var frameData:FrameData = new FrameData();
			frameData.position = new Point(rect.x, rect.y);
			frameData.bitmapData = bmd;
			frameData.offsetPoint = new Point(center.x-rect.x, center.y-rect.y);
			return frameData;
		}
	}
}